Imports Microsoft.VisualBasic
Imports System.Data.Sql
Imports System.Data.SqlClient
Imports SubSonic
Imports ClubStarterKit.Data
Imports System.Data

Namespace ClubStarterKit.Web
    Public Class News
        Public Shared Function RowCount() As Integer
            Dim qry As New Query(Tables.Announcements)
            qry.AddWhere(Announcements.Columns.Itemdate, Comparison.LessOrEquals, DateTime.Now())
            Dim count As Integer = qry.GetRecordCount()
            Return count
        End Function

        Public Shared Function NumPages() As Integer
            Dim count As Integer = RowCount()
            Return Shared_Routines.NumPages(count)
        End Function

        <System.ComponentModel.DataObjectMethodAttribute _
        (System.ComponentModel.DataObjectMethodType.Select, False)> _
    Public Shared Function List(ByVal pagenum As Integer) As IDataReader
            Dim qry As New Query(Tables.Announcements)
            qry.AddWhere(Announcements.Columns.Itemdate, Comparison.LessOrEquals, DateTime.Now())
            qry.PageSize = ClubStarterKit.Web.Settings.PageSize()
            qry.PageIndex = pagenum
            qry.OrderBy = OrderBy.Desc(Announcements.Columns.Itemdate)
            Dim rdr As IDataReader = qry.ExecuteReader()
            Return rdr
            rdr.Close()
        End Function

        Public Shared Function NextArticle(ByVal id As Integer) As String
            Dim str As String = ""

            Dim ann As New Announcements(id)
            Dim anntime As String = ann.Itemdate


            Dim qry As New Query(Tables.Announcements)
            qry.Top = "1"
            qry.OrderBy = OrderBy.Asc(Announcements.Columns.Itemdate)
            qry.AddWhere(Announcements.Columns.Itemdate, Comparison.GreaterThan, anntime)
            Dim rdr As IDataReader = qry.ExecuteReader()

            Dim NextID As Int32
            While rdr.Read
                NextID = rdr.GetInt32(0)
            End While
            rdr.Close()

            If Not IsDBNull(NextID) AndAlso Not NextID = 0 Then
                str = NextID.ToString()
            Else
                str = "-"
            End If
            Return str
        End Function

        Public Shared Function PrevArticle(ByVal id As Integer) As String
            Dim str As String = ""

            Dim ann As New Announcements(id)
            Dim anntime As String = ann.Itemdate


            Dim qry As New Query(Tables.Announcements)
            qry.Top = "1"
            qry.OrderBy = OrderBy.Asc(Announcements.Columns.Itemdate)
            qry.AddWhere(Announcements.Columns.Itemdate, Comparison.LessThan, anntime)
            Dim rdr As IDataReader = qry.ExecuteReader()

            Dim NextID As Int32
            While rdr.Read
                NextID = rdr.GetInt32(0)
            End While
            rdr.Close()

            If Not IsDBNull(NextID) AndAlso Not NextID = 0 Then
                str = NextID.ToString()
            Else
                str = "-"
            End If
            Return str
        End Function

        <System.ComponentModel.DataObjectMethodAttribute _
        (System.ComponentModel.DataObjectMethodType.Select, False)> _
        Public Shared Function GetImages(ByVal NewsID As Integer) As IDataReader
            Dim Article As New Announcements(NewsID)
            Dim albumid As Integer = Article.Albumid

            Dim qry As New Query(Tables.Images)
            qry.AddWhere(Images.Columns.Album, albumid)

            Dim rdr As IDataReader = qry.ExecuteReader()
            Return rdr
            rdr.Close()
        End Function

        <System.ComponentModel.DataObjectMethodAttribute _
        (System.ComponentModel.DataObjectMethodType.Select, False)> _
        Public Shared Function GetNewArticle(ByVal id As Integer) As Announcements
            Dim News As New Announcements(id)
            Return News
        End Function

        <System.ComponentModel.DataObjectMethodAttribute _
        (System.ComponentModel.DataObjectMethodType.Select, False)> _
        Public Shared Function ArticleDownloads(ByVal id As Integer) As IDataReader
            Dim rdr As IDataReader = SPs.DownloadsByNews(id).GetReader
            Return rdr
            rdr.Close()
        End Function

        Public Shared Sub Delete(ByVal id As String)
            Dim delqry As New Query(Tables.Announcements)
            delqry.AddWhere(Announcements.Columns.Id, id)
            delqry.QueryType = QueryType.Delete
            delqry.Execute()
        End Sub

        <System.ComponentModel.DataObjectMethodAttribute _
        (System.ComponentModel.DataObjectMethodType.Select, False)> _
        Public Shared Function AlbumList(ByVal userid As Guid) As IDataReader
            Dim sp As SubSonic.StoredProcedure = SPs.AlbumList(userid)
            Dim rdr As IDataReader = sp.GetReader()
            Return rdr
            rdr.Close()
        End Function
    End Class
End Namespace